/**
 * Created by Administrator on 2018/10/15.
 */
export default {
  data(){
    return{
      type: '',
      formValidate: {},
      nameList: []
    }
  },
  mounted (){
    if(this.nameListUrl){
      this.getNameList(this.nameListUrl);
    }
    this.type = this.$route.query.type;
    if(this.type === 'edit'){
      this.formValidate = this.$store.state.editItem;
      console.log(this.formValidate)
    }
  },
  methods: {
    //删除上传图片
    handleRemoveImg(img,imgs,string){
      var imgList = imgs;
      imgList.splice(imgs.indexOf(img), 1);
      this.$set(this.formValidate,string,imgList);
    },
    //删除上传文件
    handleRemove (file,project,string) {
      var projectAttachList = '';
      if(project==null||project==' '){
        projectAttachList = '';
      }else{
        var  fileList = project.split(',');
        projectAttachList = fileList;
        projectAttachList.splice(fileList.indexOf(file), 1);
        projectAttachList = projectAttachList.join(',');
      }
      this.$set(this.formValidate,string,projectAttachList);
    },
    // 获取名称列表
    getNameList (url) {
      this.loading = true;
      this.$get(url)
        .then((res) => {
          this.loading = false;
          this.nameList = res.data;
          console.log(res);
        }, (err) => {
          this.loading = false;
          console.log(err);
        })
    },
    handleContinue (name,url) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          if(this.formValidate.departmentIds){
            delete this.formValidate.departmentIds;
          }
          this.$post(url,this.formValidate)
            .then((res) => {
              if(res.code === 200){
                this.$Message.success('添加成功！');
                this.handleReset(name);
              }else{
                // this.$Message.error(res.msg);
              }
            }, (err) => {
              console.log(err);
            })
        } else {
          // this.$Message.error('请正确填写信息！');
        }
      })
    },

    handleSubmit (name,url,msg,callbackUrl) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          console.log(this.formValidate)
          if(this.formValidate.departmentIds){
            delete this.formValidate.departmentIds;
          }
          this.$post(url,this.formValidate)
            .then((res) => {
              if(res.code === 200) {
                this.$Message.success(msg);
                let _this = this;
                setTimeout(function () {
                  _this.$router.push({path: callbackUrl})
                }, 2000);
              }else{
                this.$Message.error(res.msg);
              }
            }, (err) => {
              console.log(err);
            })
        } else {
          this.$Message.error('请正确填写信息！');
        }
      })
    },
    handleReset (name) {
      // this.$refs[name].resetFields();
      this.formValidate = {};
    },
    prepend(value, name){
      if(value > 0) value--;
      this.$set(this.formValidate,name,value);
    },
    append(value, name){
      value = parseInt(value);
      if(value === undefined) value = 0;
      value ++;
      this.$set(this.formValidate,name,value);
    }
  }
}
